/*
	@Author: 于辉
	@Time: 2010-10
	@Tittle: login
	@Description: 登录
*/

layui.use(['form','layer','jquery'],function(){
 	var form = layui.form
 	
 	getVCode();
 	//自定义验证规则
	form.verify({
	    username: function(value){
	      if(value.length < 4){
	        return '用户名至少4个字符';
	      }
	    }
	    ,password: [
	      /^[\S]{6,20}$/
	      ,'密码必须6到20位，且不能出现空格'
	    ]
	});
	layui.$('#btn_login').on('click', function(){
	    var data = form.val('login');
		var data  = {
			username:data.userName,
		    password:data.password,
		    code:data.code,
		    uuid:$("#uuid").val()
		};
		
		ajaxSetting({
			url:getServerPath("login"),
			data:JSON.stringify(data),
			success:function(result){
				setSettings("token",result.data);
				window.location.href = "../../main.html";
			},error:function(result){
				layer.msg(result.message, {icon: 5,anim: 6});
				return false;
			}
		})
		return false;
	  });
	layui.$('#img_vcode').on('click', function(){
		getVCode();
	});
    //表单输入效果
	$(".loginBody .input-item").click(function(e){
	    e.stopPropagation();
	    $(this).addClass("layui-input-focus").find(".layui-input").focus();
	})
	$(".loginBody .layui-form-item .layui-input").focus(function(){
	    $(this).parent().addClass("layui-input-focus");
	})
	$(".loginBody .layui-form-item .layui-input").blur(function(){
	    $(this).parent().removeClass("layui-input-focus");
	    if($(this).val() != ''){
	        $(this).parent().addClass("layui-input-active");
	    }else{
	        $(this).parent().removeClass("layui-input-active");
	    }
	})
});

function getVCode(){
	ajaxSetting({
 		url:getServerPath("VCode"),
 		type:"get",
 		success:function(res){
 			console.log(res)
 			$("#img_vcode").attr("src",res.data.captchaCode)
 			$("#uuid").val(res.data.captchaGUID)
 		}
 	})
}
